//
//  MokoAppDelegate.h
//  Moko
//
//  Created by cj848 on 11. 7. 18..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SeekoTabBarController.h"
#import "SeekoIntroViewController.h"
#import "WriteViewController.h"
#import "HttpClientForPHPSession.h"
#import "LoginViewController.h"
#import "SoundSearcher.h"
#import "xPathStringVO.h"
#import "ConfigurationTableViewController.h"
#import "NSString+HTML.h"
#import "NSString+ParseQueryString.h"
#import "SeekoRootViewController.h"
#import "ConfigurationVO.h"
#import "BackupWriteTexts.h"
#import "UINavigationBar+BackgroundImage.h"

#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#define GET_APPDELEGATE MokoAppDelegate* delegate = [[UIApplication sharedApplication] delegate]
#define EUC_KR CFStringConvertEncodingToNSStringEncoding(0x422)
#define MEMO_NOTIFICATION @"MEMO_NOTIFICATION"
@interface MokoAppDelegate : NSObject <UIApplicationDelegate> {
    UITabBarController* tabBarController;
    UIViewController*   introViewController;
    WriteViewController* writeViewController;
    HttpClientForPHPSession* httpClient;
    LoginViewController* loginViewController;
    UIAlertView* loadingAlertView;
    UIActivityIndicatorView* indicator;
    // 루트 뷰 컨트롤러, 최상위 뷰로 메인 윈도우가 아닌 그냥 View 컨트롤러임
    SeekoRootViewController* rootViewController;
    ConfigurationTableViewController* configTableController;
    ConfigurationVO* configVO;
    BackupWriteTexts* backupWriteTexts;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) SeekoRootViewController* rootViewController;
@property (nonatomic, retain) UITabBarController* tabBarController;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) WriteViewController* writeViewController;
@property (nonatomic, retain) HttpClientForPHPSession* httpClient;
@property (nonatomic, retain) LoginViewController* loginViewController;
@property (nonatomic, retain) ConfigurationTableViewController* configTableController;
@property (nonatomic, retain) UIAlertView* loadingAlertView;
@property (nonatomic, retain) UIActivityIndicatorView* indicator;
@property (nonatomic, retain) ConfigurationVO* configVO;
@property (nonatomic, retain) BackupWriteTexts* backupWriteTexts;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
- (void)initManagedObject;
- (void)setDefaultConfig;
- (void)setBackupManagedObject;
@end

// NSString Category
@interface NSString (idx)
-(int)indexOf:(NSString*)c;
-(int)lastIndexOf:(NSString*)c;
@end
@implementation NSString(idx)
-(int)indexOf:(NSString*)c {
    NSRange range=[self rangeOfString:c];
    if (range.location==NSNotFound) return -1;
    return range.location;
}
-(int)lastIndexOf:(NSString*)c {
    return [self rangeOfString:c options:NSBackwardsSearch].location;
}
@end

@interface UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController;
- (id) traverseResponderChainForUIViewController;
@end

// UIView Category
@implementation UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController {
    // convenience function for casting and to "mask" the recursive function
    return (UIViewController *)[self traverseResponderChainForUIViewController];
}

- (id) traverseResponderChainForUIViewController {
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    } else if ([nextResponder isKindOfClass:[UIView class]]) {
        return [nextResponder traverseResponderChainForUIViewController];
    } else {
        return nil;
    }
}
@end